package com.example.demo.controller;

import com.example.demo.mappers.UserMapper;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MapperController {
    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private UserServiceImpl userService;

    @ResponseBody
    @RequestMapping("/getUsers")
    public List<User> selUsers() {
        return userMapper.queryUserList();
    }

//    @RequestMapping("/getUsers")
//    @ResponseBody
//    public int register(User user) {
//        return userMapper.addUser(user);
//    }

    @RequestMapping("/add")
    @ResponseBody
    public String addUser() {
        User user = new User();
        user.setSno("23434");
        user.setCno("23");
        user.setGrade(99);
        int reuslt = userService.register(user);
        if (reuslt == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }
    @RequestMapping("/upUser")
    @ResponseBody
    public String update() {
        User user = new User();
        user.setSno("1");
        user.setCno("1");
        user.setGrade(98);
        int i = userService.personalManage(user);
        if (i == 1) {
            return "更新成功";
        } else {
            return "更新失败";
        }
    }
    @ResponseBody
    @RequestMapping("deleteUser")
    public String delete() {
        User user = new User();
        user.setCno("1");
        int i = userService.delete(user);
        if (i == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }
    @ResponseBody
    @RequestMapping("/hi")
    public String syaHello(@RequestBody User user) {
        System.out.println(user);
        return user.getCno();
    }
}
